package com.sgb.kjwl.view.ui.fragment.business;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMemberDetailBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.business.MemberDetailEntity;
import com.sgb.kjwl.view.ui.adapter.business.ImgListAdapter;
import com.sgb.kjwl.viewmodel.business.MemberDetailFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MLog;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.inject.Inject;

public class MemberDetailFragment extends AppFragment {
    public static final String TAG = "";
    private FragmentMemberDetailBinding binding;
    @Inject
    MemberDetailFragModel viewModel;
    private String id;
    private ImgListAdapter imgListAdapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static MemberDetailFragment newInstance(String id) {
        MemberDetailFragment fragment = new MemberDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_detail, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        if(bundle!=null){
            id = bundle.getString("id");
        }

        if (imgListAdapter == null) {
            imgListAdapter = new ImgListAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rvImg.setPullRefreshEnabled(false);
        binding.rvImg.setLoadingMoreEnabled(false);
        binding.rvImg.setLayoutManager(layoutManager);
        binding.rvImg.setAdapter(imgListAdapter);
    }

    public void initData() {
        viewModel.getMemberCompanyDetail(id);
    }

    public void showData(MemberDetailEntity memberDetailEntity){
        binding.tvAddress.setText(memberDetailEntity.getAddress());
        binding.tvBusiness.setText(memberDetailEntity.getMainBusiness());
        binding.tvCode.setText(memberDetailEntity.getCodeAttr());
        binding.tvComName.setText(memberDetailEntity.getCompanyName());
        binding.tvComStyle.setText(memberDetailEntity.getCompanyType());
        binding.tvComType.setText(memberDetailEntity.getServiceType());
        binding.tvContact.setText(memberDetailEntity.getConcat());
        binding.tvFaren.setText(memberDetailEntity.getLegalPerson());
        binding.tvGuPhone.setText(memberDetailEntity.getTel());
        binding.tvMail.setText(memberDetailEntity.getEmail());
        binding.tvMobile.setText(memberDetailEntity.getConcatNum());
        binding.tvPrice.setText(memberDetailEntity.getRegisteredCapital());
        binding.tvTime.setText(memberDetailEntity.getEstablishmentDate());

        //web
        Document htmlResult = Jsoup.parseBodyFragment(memberDetailEntity.getCompanyIntroduce());
        Elements elements = htmlResult.select("body");
        for (Element each : elements) {
            each.attr("style", "font-size:34px");
        }
        elements = htmlResult.getElementsByTag("p");
        for (Element element : elements) {
            element.attr("style", "color:#666666");
        }
        MLog.d("", htmlResult.html());

        binding.webIntroduction.loadData(htmlResult.html(), "text/html; charset=UTF-8", null);
        binding.webIntroduction.getSettings().setJavaScriptEnabled(true);
        binding.webIntroduction.getSettings().setSupportZoom(false);
        binding.webIntroduction.getSettings().setBuiltInZoomControls(false);
        binding.webIntroduction.getSettings().setUseWideViewPort(true);
        binding.webIntroduction.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        binding.webIntroduction.getSettings().setLoadWithOverviewMode(true);
        binding.webIntroduction.setBackgroundColor(getResources().getColor(R.color.color_F5F5F5)); // 设置背景色
        binding.webIntroduction.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                binding.webIntroduction.loadUrl("javascript:document.body.style.padding=\"3.2%\"; void 0");
            }
        });

        //图片
        if (memberDetailEntity.getImages() != null && memberDetailEntity.getImages().size()>0){
            imgListAdapter.setDatas(memberDetailEntity.getImages());
        }
    }
}
